// 监听安装事件
self.addEventListener('install', (event) => {
  self.skipWaiting()
})

// 监听激活事件
self.addEventListener('activate', (event) => {
  event.waitUntil(clients.claim())
})

// 监听通知点击事件
self.addEventListener('notificationclick', (event) => {
  event.notification.close()
  
  // 处理通知动作
  if (event.action) {
    console.log('通知动作:', event.action)
  }
  
  // 打开或聚焦窗口
  event.waitUntil(
    clients.matchAll({ type: 'window' }).then((clientList) => {
      for (const client of clientList) {
        if (client.url === '/' && 'focus' in client) {
          return client.focus()
        }
      }
      if (clients.openWindow) {
        return clients.openWindow('/')
      }
    })
  )
})

// 监听通知关闭事件
self.addEventListener('notificationclose', (event) => {
  console.log('通知已关闭')
})

// 监听消息事件
self.addEventListener('message', (event) => {
  if (event.data && event.data.type === 'CLOSE_ALL_NOTIFICATIONS') {
    self.registration.getNotifications().then((notifications) => {
      notifications.forEach((notification) => notification.close())
    })
  }
}) 